<script setup lang="ts" name="BetterScroll">
import { useElementSize } from '@vueuse/core'
import BScroll from '@better-scroll/core'
import type { Options } from '@better-scroll/core'

const props = defineProps<Props>()
// BetterScroll options @link https://better-scroll.github.io/docs/zh-CN/guide/base-scroll-options.html
interface Props {
	options: Options
}

const bsWrap = ref<HTMLElement>()
const bsContent = ref<HTMLElement>()
const { width: wrapWidth } = useElementSize(bsWrap)
const { width, height } = useElementSize(bsContent)

const instance = ref<BScroll>()
const isScrollY = computed(() => Boolean(props.options.scrollY))

function initBetterScroll() {
	if (!bsWrap.value) return
	instance.value = new BScroll(bsWrap.value, props.options)
}

// refresh BS when scroll element size changed
watch([() => wrapWidth.value, () => width.value, () => height.value], () => {
	instance.value?.refresh()
})

onMounted(() => {
	initBetterScroll()
})

defineExpose({ instance })
</script>

<template>
	<div ref="bsWrap" class="h-full text-left">
		<div ref="bsContent" class="inline-block" :class="{ 'h-full': !isScrollY }">
			<slot></slot>
		</div>
	</div>
</template>

<style scoped></style>
